// Stream I/O Routines
//

#include <stdio.h>
#include <stdlib.h> 
#include <stdarg.h>
#include <string.h>
#include "Common/Debug.h"
#include "FileSys.h"
//#include "minilzo.h"
#include "Memory.h"	
#include "Macros.h"

static char FS_ROOT[128] = "/.";

static char FS_FOLDER[30] = "/res";

static char _szPathname[_MAX_PATH];
static char *_pszFilename = NULL;

BOOL fs_Initialize(void)
{
	// set root of file system
	#if defined __SD_CARD
		//fs_SetRoot("/mnt/mtd2");/////////hangsheng 20080228
		//fs_SetRoot("/mnt/mtd3");
		//fs_SetRoot("/mnt/sd");
		fs_SetRoot("/mnt/usb");
	#else
		fs_SetRoot("/mnt/cf");
	#endif

	return TRUE;
}

void fs_SetRoot(const char *path)
{
	if (path == NULL || path[0] == '\0')
		return;

	strcpy(FS_ROOT, path);

	strcpy(_szPathname, FS_ROOT);

	strcat(_szPathname, FS_FOLDER );
	_pszFilename = _szPathname + strlen(FS_ROOT) + strlen(FS_FOLDER);

	if (*(_pszFilename - 1) == '/')
		_pszFilename--;
}


const char*	fs_GetRoot(void)
{
	return FS_ROOT;
}

const char* fs_GetFullPath(const char *filename)
{
	ASSERT_DEBUG(filename || filename[0] != '\0');

	if (filename[0] == '/')
		strcpy(_pszFilename, filename);
	else
	{
		*_pszFilename = '/';
		strcpy(_pszFilename + 1, filename);
	}

	return _szPathname;
}

FILE* fs_Open(const char* filename, char* mode)
{
	FILE *fp;

	const char *pathname = fs_GetFullPath(filename);

	if( pathname == NULL )
		LOG("pathname = null \n");
printf("awei_pathname:%s\n",pathname);
	fp = fopen(pathname, mode);

	if (fp == NULL)
	{
		LOG("fs_Open: open '%s'(%s) failed!\n", pathname, mode);
		return NULL;
	}

	fs_Seek(fp, 0, SEEK_SET);

	return fp;
}

int fs_Close_(FILE* stream)
{
	return fclose(stream);
}

void fs_Close(FILE** stream)
{
	if (*stream != NULL)
	{
		fclose(*stream);
		*stream = NULL;
	}
}

size_t fs_Read(void* buffer, size_t size, FILE* stream)
{
	ASSERT_DEBUG(buffer && stream);

	if (buffer == NULL || size == 0 || stream == NULL)
	{
		LOG("fs_Read: invalid params %x %x %x!\n", buffer, size, stream);
		return -1;
	}

	if (size != fread(buffer, 1, size, stream))
	{
		LOG("fs_Read: invalid reading!\n");
		return -1;
	}

	return size;
}

size_t fs_Write(const void* buffer, size_t size, FILE* stream)
{
	ASSERT_DEBUG(buffer && stream);

	if (buffer == NULL || size == 0 || stream == NULL)
	{
		LOG("fs_Write: invalid params!\n");
		return -1;
	}

	if (size != fwrite(buffer, 1, size, stream))
	{
		LOG("fs_Write: invalid writing!\n");
		return -1;
	}

	return size;
}

int fs_Seek(FILE* stream, long offset, int orgin)
{
	ASSERT_DEBUG(stream);

	if (fseek(stream, offset, orgin) != 0)
	{
		LOG("fs_Seek: failed!!\n");
		return -1;
	}

	return 0;
}

UInt32 fs_GetFileLen(FILE* stream)
{
	long cur;
	UInt32 len;

	ASSERT_DEBUG(stream);
	
	cur = ftell(stream);
	fseek(stream, 0, SEEK_END);
	len = ftell(stream);
	fseek(stream, cur, SEEK_SET);

	return len;
}

// Get root directory
char* fs_GetRootDir(void)
{
	return FS_ROOT;
}

int fs_Printf(FILE* stream,...)
{
#define MAX_STR_LEN 512
	static char str[MAX_STR_LEN];
	char *format;
	va_list arg;
	va_start(arg, stream);
	format = va_arg(arg, char*);
	vsprintf(str, format, arg);
	va_end(arg);

	return fprintf(stream, str);
}
